<?php
require_once 'application/models/Base/Model.php';

class User_Admin extends Base_Model
{
	protected $_adminId;
	  
    /**
     * Возвращает объект User_Admin если такой логин существует, и false в
     * противном случае
     * 
     * @param string $username
     * @return int
     */
    public static function findAdminIdByLogin($username)
    {
      	$admin_id = empty($username) ? null : Framework_Registry::get('dbh')->selectCell(
			'SELECT admin_id 
				FROM admin 
				WHERE LOWER(admin_login)=LOWER(?)', $username);
        return empty($admin_id) ? null : $admin_id;
    }	

	/**
	 * удаляем пользователя из таблички user
	 * @param int $user_id  id пользователя для удаления 
	 * @return 
	 */
	public static function setUserDelete($user_id = 0)
	{
		if (!$user_id) return false;
		return Framework_Registry::get('dbh')->query('DELETE FROM user 
														WHERE user_id = ?',$user_id);
	}
	
	/**
	 * увеличиваем рейтинг пользователя
	 * @param $id int
	 */
	public static function increaseUserRating($user_id)
	{
		$rait = Framework_Registry::get('dbh')->
			selectRow("SELECT max(user_rating_count) as maxrait
						FROM user");
		$rait = ($rait) ? $rait['maxrait'] : 0;
		for ($i=0; $i<=$rait; $i++)
		{
			Framework_Registry::get('dbh')->
				query("INSERT INTO rating (rating_user_id_to,rating_score)
						VALUES (?,?)",$user_id,10);
		}
	}
	
	/**
	 * меняем статус показа пользователя на главной
	 * @return 
	 * @param int $user_id
	 */
	public static function showOnHome($user_id)
	{
		Framework_Registry::get('dbh')->
			query("UPDATE user SET
					user_show_on_home = NOT user_show_on_home
					WHERE user_id = ?",$user_id);	
	}
	
	/**
	* 	check for login-pass valid.
	*	@param varchar $login
	*	@param varchar $passs
	*	@return  $admin 
	*/
    public function getIsValidAdmin($login, $pass)
    {
        $rows = $this->_dbh->select("SELECT admin_id 
										FROM admin
										WHERE admin_login = '$login' 
										AND admin_pass = '$pass'
										LIMIT 1");
        $admin = 0;
		if ($rows)
		{
			$admin = $rows[0]['admin_id'];
		}
		$this->_adminId = $admin;
        return $admin;
    }
    
	/**
	* what current admin can do
	*/
	public function getPossibilities($id)
	{
		$rows = $this->_dbh->select("SELECT admgroup_priv 
									FROM admin_view
									WHERE admin_id = ?",$id);
		$priv = ($rows) ? $rows[0]['admgroup_priv'] : 0;
		$possibl = $this->_dbh->select("SELECT admpriv_id, admpriv_comment, admpriv_key
										FROM admpriv
										WHERE admpriv_key&$priv");
		return $possibl;
	}
	
	/**
	* what's admin group?
	*/
	public function getAdminGroup($id)
	{
		$rows = $this->_dbh->select("SELECT admgroup_name 
									FROM admin_view
									WHERE admin_id = ?",$id);
		$group = ($rows)?$rows[0]['admgroup_name'] : '';
		return $group;
	}
	
	public static function getAdminGroupId($id)
	{
		$rows = Framework_Registry::get('dbh')->select("SELECT admin_group_id 
									FROM admin_view
									WHERE admin_id =?", $id);
		$admId = ($rows)?$rows[0]['admin_group_id'] : 0;
		return $admId;
	}
	
	/**
	* get the list of all privilegies
	*/
	public function getAllPrivs()
	{
		$rows = $this->_dbh->select("SELECT admpriv_comment
									FROM admpriv
									ORDER BY admpriv_id");
		return $rows;
	}
	
	/**
	* sets the fact of login
	*/
	public function setLogin($id)
	{
		$this->_dbh->query("UPDATE admin 
								SET admin_last_login = CURRENT_TIMESTAMP 
								WHERE admin_id = ?",$id);
	}
	
	public function getAllAdmins($group)
	{
		$rows = $this->_dbh->select("SELECT admin_id, admin_login, admin_last_login
										FROM admin_view
										WHERE admin_group_id = ?",$group);
		return $rows;
	}
	
	public function setNewAdmin($login, $pass,$group)
	{
		$res = @$this->_dbh->query("INSERT INTO admin (admin_login, admin_pass, admin_group_id)
							VALUES (?,?,?)",$login,$pass,$group);
		return $res;
	}
	
	public function setDelete($id)
	{
		$res = @$this->_dbh->query("DELETE FROM admin WHERE admin_id = ?",$id);
		return $res;
	}
	
	/**
	 * definition of group
	 * @return $return array group id and  group name
	 * @param $arg0 Object
	 */
	public function getWho($arg0)
	{
		$who = get_value($arg0);
		$return = array();
		switch ($who)
		{
			case 'admin': $return['who'] = 'admin';
							$return['groupId'] = 1;
							break;
			case 'moderator': 
			case 'moder':	$return['who'] = 'moderator';
							$return['groupId'] = 2;
							break;
			case 'animator':
			case 'anim':	$return['who'] = 'animator';
							$return['groupId'] = 3;
							break;
			case 'user':	$return['who'] = 'user';
							$return['groupId'] = 0;
							break;
			default: $return['who'] = 'animator';
					$return['groupId'] = 3;
		}
		return $return;
	}
}